<?php
class Sites extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @return Organizations the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'sites';
	}
	
	public function relations()
	{
		return array(
			'auctions'=>array(self::HAS_MANY, 'Auctions', 'site_ID'),
			'organization'=>array(self::BELONGS_TO, 'Organizations', 'organization_ID'),
			'tanks'=>array(self::HAS_MANY, 'Tanks', 'site_ID'),
			'sitetoolRelation'=>array(self::HAS_MANY, 'SiteToolRelation', 'site_ID'),
			'rack'=>array(self::BELONGS_TO, 'Racks', 'home_rack_ID'),
		);
	}
	
	public function rules()
	{
		return array(
			array('site_name, ust_facility_ID, organization_ID, address1, city, state, zip, county,' .
					' contact_name, contact_cell_number, phone_number_of_site, after_hour_number, gated, ' .
					' gate_opens, gate_closes, security_on_premises, security_code, open_time, close_time', 'required', 'on'=>'create'),
			array('site_name, address1, city, state, zip, county', 'required', 'on'=>'update-profile'),
			array(' contact_name, contact_cell_number, phone_number_of_site, after_hour_number, gated, ' .
					' gate_opens, gate_closes, security_on_premises, security_code, open_time, close_time', 'required', 'on'=>'update-details'),
			array('ust_facility_ID', 'numerical', 'min'=>0, 'integerOnly'=>true, 'on'=>'create, update-profile, update-details'),
			array('gated, security_on_premises', 'boolean', 'falseValue'=>0, 'trueValue'=>1, 'on'=>'create, update-details'),
		//	array('gate_opens, gate_closes, open_time, close_time', 'date', 'format'=>'hh:mm', 'on'=>'create, update-details'),
			array('gate_opens, gate_closes, open_time, close_time', 'match', 'pattern'=>'/^([0-1][0-9]|[2][0-3]):([0-5][0-9])$/', 'on'=>'create, update-details'),
			array('contact_cell_number, phone_number_of_site, after_hour_number', 'match', 'pattern'=>'/^\d{10}$/', 'on'=>'create, update-details'),
			array('contact_name', 'length', 'max'=>30, 'on'=>'create, update-details'),
			array('site_name, city, county, geocode', 'length', 'max'=>30, 'on'=>'create, update-profile'),
			array('security_code', 'length', 'max'=>20, 'on'=>'create, update-profile, update-details'),
			array('address1, address2', 'length', 'max'=>50, 'on'=>'create, update-profile'),
			array('state', 'length', 'max'=>2, 'on'=>'create, update-profile'),
			array('zip', 'length', 'max'=>5, 'on'=>'create, update-profile'),
			array('zip', 'match', 'pattern'=>'/^\d{5}$/', 'message'=>'the zip code format is not correct', 'on'=>'create, update-profile'),
			array('directions, notes', 'length', 'max'=>200, 'on'=>'create, update-details'),
		);
	}
	
	public function loadCounties()
	{
		$connection = Yii::app()->db;
		$sql = "SELECT county FROM ".$this->tableName()." group by county";
		$command = $connection->createCommand($sql);
		$dataReader = $command->query();
		$res=array();
		foreach($dataReader as $row)
		{
			$key=$row['county'];
			$res[$key]=$row['county'];
		}
		return $res;
	}
	
	public function getSiteName($siteID) {
		$sql = "SELECT site_name FROM ".$this->tableName()." WHERE site_ID='".$siteID."'";
		$resultset = $this->findAllBySql($sql);
		
		foreach ($resultset as $row) {
			$siteName = $row->site_name;
		}
		
		return $siteName;
	}
	
	public function getActiveSites() {
		$sql = "SELECT * FROM ".$this->tableName()." WHERE active='1'";
		$resultset = $this->findAllBySql($sql);
		
		return $resultset;
	}
	
	public function getSiteID($userID) {
		$organizationID = Users::model()->getOrganizationID($userID);
		
		$sql = "SELECT site_ID FROM ".$this->tableName()." WHERE organization_ID='".$organizationID."'";
		$resultset = $this->findAllBySql($sql);
		
		foreach ($resultset as $row) {
			$siteID = $row->site_ID;
		}
		
		return $siteID;
	}
	
	public function siteExists($siteName) {
		$sql = "SELECT site_ID FROM ".$this->tableName()." WHERE site_name='".$siteName."'";
		$resultset = $this->findAllBySql($sql);
		
		if (empty($resultset))
			return false;
		else
			return true;
	}
}
?>
